
define(['ionic', 'controllers'], function () {
    angular.module('U9Mobile.controllers')
        .controller('HomeCtrl', ['$scope', 'AppManager', 'UserService', 'UtilService', 'AppConstants',
            function ($scope, AppManager, UserService, UtilService, AppConstants) {
                $scope.data = {
                    appsArray: [],
                    activeApp: ''
                };

                $scope.activeApp = function (appId) {
                    $scope.data.activeApp = appId;
                };
                $scope.unActiveApp = function () {
                    $scope.data.activeApp = '';
                };
                $scope.clickApp = function (app) {
                    if (app.id === AppConstants.ADD_APP_IMAGE.id) {
                        UtilService.goState('app.download');
                        return;
                    }
                    if(!app.u9){
                        UtilService.alert("该子应用需要更新到最新版才可用");
                        return;
                    }
                    if(app.u9>AppConstants.APP_VERSION){
                        UtilService.alert("需要下载最新版的工作台");
                        return;
                    }

                    var logined = UserService.getUserToken().logined;
                    if (!logined) {
                        UtilService.confirm('请先登录！').then(function (res) {
                            if (res) {
                                UtilService.goState('app.login');
                            }
                        });
                        return;
                    }
                    //UtilService.showLoading('权限验证中...');
                    //AppManager.checkLicense(app.id).then(function (data) {
                    //    if (data.flag === '000000') {
                            AppManager.setActiveAppId(app.id);
                            if (window.Analytics) {
                                window.Analytics.analytics("clickApp",app.name);
                            }
                            UtilService.goState('app.temp');
                    //    } else {
                    //        UtilService.alert(data.desc || '权限验证失败');
                    //    }
                    //}, function (error) {
                    //    UtilService.alert('权限验证失败');
                    //}).finally(function () {
                    //    UtilService.hideLoading();
                    //});
                };

                AppManager.loadingAppPromise.then(function () {
                    var apps = AppManager.getInstallApps();
                    apps.push(AppConstants.ADD_APP_IMAGE);
                    $scope.data.appsArray = formatApps(apps);
                });

                function formatApps (apps) {
                    var maxNum = AppConstants.APP_NUM_PER_PAGE,
                        j = 0,
                        temp = [],
                        results = [];
                    for (var i in apps) {
                        temp.push(apps[i]);
                        j++;
                        if (j === maxNum) {
                            j = 0;
                            results.push(temp);
                            temp = [];
                        }
                    }
                    if (apps.length % maxNum !== 0) {
                        results.push(temp);
                    }
                    return results;
                }
            }
        ]);
});